package com.fuang.enumdemo;

import java.util.EnumMap;
import java.util.Map;

/**
 * Created by Fuang on 2017-7-1.
 * EnumMap 的使用
 */
public class EnumMapDemo {

    public enum Phase{
        SOLID, LIQUID, GAS;

        public enum Transition{
            MELT(SOLID, LIQUID),
            FREEZE(GAS, SOLID),
            BOIL(LIQUID, GAS);

            private final Phase src;
            private final Phase dst;

            Transition(Phase src, Phase dst) {
                this.src = src;
                this.dst = dst;
            }

            public static final Map<Phase, Map<Phase, Transition>> m = new EnumMap<Phase, Map<Phase, Transition>>(Phase.class);

            static {
                for (Phase p : Phase.values()) {
                    m.put(p, new EnumMap<Phase, Transition>(Phase.class));
                }

                for (Transition transition : Transition.values()) {
                    m.get(transition.src).put(transition.dst, transition);
                }
            }

            public static Transition from (Phase src, Phase dst) {
                return m.get(src).get(dst);
            }
        }

    }
}
